package com.lwy.util;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lwy.entity.BookNove;
import com.lwy.entity.BookNovelChapter;
import com.lwy.service.impl.BookNovelChapterServiceImpl;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.*;

public class ChapterFilter {


    public static String change(String path) {
        File file = new File(path);

        StringBuilder result = new StringBuilder();
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
            String s = null;
            while ((s = br.readLine()) != null) {//使用readLine方法，一次读一行
                result.append(System.lineSeparator() + s);
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.toString();
    }


    public static  String getTile(String path){
        int index = path.indexOf("第");
        String substring = path.substring(index);
        if(substring.endsWith(".txt")){
            String[] split = substring.split(".txt");
            for (String str : split) {
                return str;
            }
        }
        return null;
    }

    public static String getUrl(String title,String chapter){
        String path="D:\\Temp\\xm_zhzw\\src\\main\\resources\\static\\chapter\\"+title+".txt";
        BufferedWriter bw=null;
        try{
            File file=new File(path);
            if(file.exists()){
                file.createNewFile();
            }
            FileWriter fw =new FileWriter(file);
            bw=new BufferedWriter(fw);
            bw.write(title+"\n"+chapter);

        }catch(IOException e){
            e.printStackTrace();
        }finally {
            try {
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return path;
    }

    public static void setEntity(BookNovelChapter bookNovelChapter){
        String change = ChapterFilter.change(bookNovelChapter.getReurl());
        String title = ChapterFilter.getTile(bookNovelChapter.getReurl());
        bookNovelChapter.setChapter(change);
        bookNovelChapter.setTitle(title);
    }
    public static long getWord(String path){
        File file = new File(path);
        String b = "[`~!@#$%^&*()+=|{}':;',\\\\[\\\\].<>/?~！@#￥%……&*()——+|{}【】‘；：”“’。，、？\\\\\\\\]";
        InputStream bis = null;
        long a = 0;
        try {
            bis = new BufferedInputStream(new FileInputStream(file));
            byte[] bytes = new byte[3];
            int len;
            while((len = bis.read(bytes)) != -1) {
                //逐个字节写入
                String s = new String(bytes);
                boolean matches = s.matches(b);
                if(matches||s.equals(" ")){
                    continue;
                }else{
                    a++;
                }
            }
        }  catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return a;
    }

    public static String pageFormat(String str){
        String str1="";
        String str2 ="";
        StringBuffer sb = new StringBuffer();
        char[] chars = str.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if(chars.length==3){
                if(i==chars.length-3){
                    char aChar = chars[i];
                    sb.append("0");
                    sb.append(aChar);
                }
            }else if(chars.length>3){
                if(i==chars.length-3){
                    char aChar = chars[i];
                    sb.append(aChar);
                }
                if(i==chars.length-4){
                    char aChar = chars[i];
                    sb.append(aChar);
                }
            }
        }
        if(str.length()>3){
            String substring = str.substring(str.length() - 4);
            String[] split = str.split(substring);
            for (String s : split) {
                str1=s;
            }
        }
        if(str1.equals("")){
            str2="0"+"."+sb.toString()+"万字";
        }else{
            str2=str1 +"."+ sb.toString()+"万字";
        }
        return str2;
    }
}

